package lims.client;

import javax.swing.JDialog;
import javax.swing.JLabel;

import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JPanel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JCheckBox;

public class DialogPrinter extends JDialog{
	/**
	 * 
	 */
	private static final long serialVersionUID = -8244901054080760633L;
	private JTextField text_name;
	private JTextField text_description;
	public boolean modalResult=false;
	private LimsPrinter printer;
	private JTextField text_scale;
	
	public DialogPrinter(LimsPrinter printer,JFrame owner) {
		super(owner);
		this.printer=printer;
//		setPreferredSize();
		setSize(new Dimension(300, 259));
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.rowHeights = new int[] {30, 30, 30, 30, 50};
		gridBagLayout.columnWidths = new int[] {30, 30, 0, 30};
		gridBagLayout.columnWeights = new double[]{1.0, 1.0, 0.0, 1.0};
		gridBagLayout.rowWeights = new double[]{1.0, 0.0, 0.0, 1.0, 0.0, 0.0};
		getContentPane().setLayout(gridBagLayout);
		
		JLabel lblNewLabel = new JLabel("名称:");
		GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
		gbc_lblNewLabel.anchor = GridBagConstraints.EAST;
		gbc_lblNewLabel.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel.gridx = 0;
		gbc_lblNewLabel.gridy = 0;
		getContentPane().add(lblNewLabel, gbc_lblNewLabel);
		
		text_name = new JTextField();
		GridBagConstraints gbc_text_name = new GridBagConstraints();
		gbc_text_name.insets = new Insets(0, 0, 5, 5);
		gbc_text_name.fill = GridBagConstraints.HORIZONTAL;
		gbc_text_name.gridx = 1;
		gbc_text_name.gridy = 0;
		getContentPane().add(text_name, gbc_text_name);
		text_name.setColumns(10);
		
		JPanel panel_1 = new JPanel();
		GridBagConstraints gbc_panel_1 = new GridBagConstraints();
		gbc_panel_1.insets = new Insets(0, 0, 5, 0);
		gbc_panel_1.fill = GridBagConstraints.BOTH;
		gbc_panel_1.gridx = 3;
		gbc_panel_1.gridy = 0;
		getContentPane().add(panel_1, gbc_panel_1);
		
		JLabel lblNewLabel_1 = new JLabel("说明:");
		GridBagConstraints gbc_lblNewLabel_1 = new GridBagConstraints();
		gbc_lblNewLabel_1.anchor = GridBagConstraints.EAST;
		gbc_lblNewLabel_1.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel_1.gridx = 0;
		gbc_lblNewLabel_1.gridy = 1;
		getContentPane().add(lblNewLabel_1, gbc_lblNewLabel_1);
		
		text_description = new JTextField();
		GridBagConstraints gbc_text_description = new GridBagConstraints();
		gbc_text_description.insets = new Insets(0, 0, 5, 5);
		gbc_text_description.fill = GridBagConstraints.HORIZONTAL;
		gbc_text_description.gridx = 1;
		gbc_text_description.gridy = 1;
		getContentPane().add(text_description, gbc_text_description);
		text_description.setColumns(10);
		
		JLabel lblNewLabel_2 = new JLabel("打印机:");
		GridBagConstraints gbc_lblNewLabel_2 = new GridBagConstraints();
		gbc_lblNewLabel_2.anchor = GridBagConstraints.EAST;
		gbc_lblNewLabel_2.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel_2.gridx = 0;
		gbc_lblNewLabel_2.gridy = 2;
		getContentPane().add(lblNewLabel_2, gbc_lblNewLabel_2);
		
		JButton btnSetup = new JButton("设置");
		btnSetup.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				DialogPrinter.this.setup();
			}
		});
		GridBagConstraints gbc_btnSetup = new GridBagConstraints();
		gbc_btnSetup.insets = new Insets(0, 0, 5, 5);
		gbc_btnSetup.anchor = GridBagConstraints.WEST;
		gbc_btnSetup.gridx = 1;
		gbc_btnSetup.gridy = 2;
		getContentPane().add(btnSetup, gbc_btnSetup);
		
		JLabel label = new JLabel("输出比例:");
		GridBagConstraints gbc_label = new GridBagConstraints();
		gbc_label.anchor = GridBagConstraints.EAST;
		gbc_label.insets = new Insets(0, 0, 5, 5);
		gbc_label.gridx = 0;
		gbc_label.gridy = 3;
		getContentPane().add(label, gbc_label);
		
		text_scale = new JTextField();
		GridBagConstraints gbc_text_scale = new GridBagConstraints();
		gbc_text_scale.insets = new Insets(0, 0, 5, 5);
		gbc_text_scale.fill = GridBagConstraints.HORIZONTAL;
		gbc_text_scale.gridx = 1;
		gbc_text_scale.gridy = 3;
		getContentPane().add(text_scale, gbc_text_scale);
		text_scale.setColumns(10);
		
		JPanel panel = new JPanel();
		GridBagConstraints gbc_panel = new GridBagConstraints();
		gbc_panel.insets = new Insets(0, 0, 5, 0);
		gbc_panel.gridwidth = 4;
		gbc_panel.fill = GridBagConstraints.BOTH;
		gbc_panel.gridx = 0;
		gbc_panel.gridy = 5;
		getContentPane().add(panel, gbc_panel);
		
		JButton btn_ok = new JButton("确定");
		btn_ok.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				DialogPrinter.this.onOk();
			}
		});
		panel.add(btn_ok);
		
		JButton btnNewButton_2 = new JButton("取消");
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		panel.add(btnNewButton_2);
		
		text_name.setText(printer.name);
		text_scale.setText(String.valueOf(printer.scale));
		text_description.setText(printer.description);
		
	}
	
	protected void setup() {
		try{
			printer.setUp();
		}catch(Exception e){
			JOptionPane.showMessageDialog(this,e.getMessage());
		}
	}
	
	protected void onOk() {
		if(!text_name.getText().equals(printer.name)){
			if(JOptionPane.showConfirmDialog(this,"修改名称将可能导致打印引用失败，请慎重考虑是否继续操作","警告",JOptionPane.YES_NO_OPTION)==JOptionPane.YES_OPTION){
				printer.name=text_name.getText();
			}else return;
		}
		
		printer.description=text_description.getText();
		printer.scale=Double.valueOf(text_scale.getText());
		modalResult=true;	
		dispose();
	}

}
